//
//  MyDiscountViewController.m
//  PTravel
//
//  Created by dllo on 15/11/29.
//  Copyright © 2015年 李涵. All rights reserved.
//

#import "MyDiscountViewController.h"
#import "DiscountDetailViewController.h"

@interface MyDiscountViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation MyDiscountViewController

- (void)dealloc
{
    [_myTableView release];
    [_arr release];
    [super dealloc];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = CELLCOLOR;
    UIImageView *bgImage = [[UIImageView alloc] initWithFrame:BOUNDS];
    bgImage.image = [UIImage imageNamed:@"placeholder.jpg"];
    [self.view addSubview:bgImage];
    [bgImage release];
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, BOUNDS.size.width, BOUNDS.size.height) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.myTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.myTableView];
    [_myTableView release];
#pragma mark === 查询收藏
    self.arr = [NSMutableArray array];
    self.arr = [[DataBaseHandle shareDataBase] selectDiscountSql:@"select * from discount"];
    if (self.arr.count == 0) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, BOUNDS.size.height/2-20, BOUNDS.size.width, 40)];
        label.backgroundColor = [UIColor colorWithWhite:0.514 alpha:0.500];
        label.text = @"您并未收藏任何折扣，快去看看吧";
        label.textAlignment = 1;
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont systemFontOfSize:20];
        [self.view addSubview:label];
    }
    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.title = @"我的目的地";
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text = [[self.arr objectAtIndex:indexPath.row] titleName];
    cell.textLabel.textColor = TEXTCOLOR;
    cell.backgroundColor = CELLCOLOR;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DiscountDetailViewController *des = [[DiscountDetailViewController alloc] init];
    [self.navigationController pushViewController:des animated:YES];
    des.url = [[self.arr objectAtIndex:indexPath.row] url];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        [[DataBaseHandle shareDataBase] deleteDataTableSql:[NSString stringWithFormat:@"DELETE from discount where url = '%@'",[[self.arr objectAtIndex:indexPath.row] url]]];
        [self.arr removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationMiddle];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
